<?php
/**
 * Created by PhpStorm.
 * User: win10
 * Date: 2017/12/19
 * Time: 16:55
 */

namespace api\controllers;

use common\models\BaseModels;
use common\services\OrderService;
use common\services\tracking\EventService;
use common\models\kaoguobao\ParamsInfo;
use common\services\config\Config;
use common\helpers\StringHelper;

class GoodsInfoController extends BaseController{
    public function init(){
        parent::init();
    }

    /**
     * 产品首页
     * @param  key     get/post    项目简称
     * @return array
     */
    public function actionGoodsHomePage()
    {
        $wechatId   = $this->user_id;
        if(!$wechatId){
            return $this->errorResult(Config::$notLogin);
        }
        $key  = trim($this->request->post('key','kgb'));
        if(empty($key)){
            return $this->errorResult(Config::$parameter);
        }
        EventService::clickEventTracking($key);
        $result = OrderService::goodsInfoService($key);
        $result['user_id'] = $wechatId;
        $result['wechat_id'] = \Yii::$app->session['uid'];
        return $this->endReponse('0',$result);
    }

    /**
     * 产品信息接口（通用）
     * @param  key     get/post    项目简称
     * @return array
     */
    public function actionGoodsList()
    {
//        $wechatId   = $this->user_id;
//        if(!$wechatId){
//            return $this->errorResult(Config::$notLogin);
//        }
        $key  = trim($this->request->post('key'));
        if(empty($key)){
            return $this->errorResult(Config::$parameter);
        }
        $data = ParamsInfo::find()
                ->where(['key' => $key])
                ->select('label,content')
                ->orderBy('sort asc')
                ->asArray()
                ->all();
        if(empty($data)){
            return $this->errorResult(Config::$notData);
        }
        // 拼装前端所需数组格式
        $info = [
            'head' => BaseModels::GOODS_DATA,
            'item' => $data,
        ];
        return $this->endReponse('0',$info);
    }
}